/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewUserAdvSys.java
 * 模块说明：	
 * 修改历史：
 * 2006-9-13 - lxm - 创建。
 */
package com.hd123.rumba.action.user;

import com.hd123.rumba.cartridge.FVRumbaAdvSys;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.sql.biz.BSQLSubquery;
import com.hd123.rumba.user.CPermission;
import com.hd123.rumba.user.CUser;
import com.hd123.rumba.user.PE;

/**
 * @author lxm
 * 
 */
public class ViewUserAdvSys extends BaseViewUser {

  public static final String CAPTION_VIEW = "用户维护(高级)";

  /* 权限常量 */
  public static final String FUNCV_PREFIX = FVRumbaAdvSys.FUNCV_PREFIX;
  public static final String PERMISSION_CREATE = FUNCV_PREFIX + ".用户.新建权";
  public static final String PERMISSION_VIEW = FUNCV_PREFIX + ".用户.查看权";
  public static final String PERMISSION_EDIT = FUNCV_PREFIX + ".用户.编辑权";
  public static final String PERMISSION_DELETE = FUNCV_PREFIX + ".用户.删除权";
  public static final String PERMISSION_RECYCLE_VIEW = FUNCV_PREFIX + ".用户.回收站.查看权";
  public static final String PERMISSION_RECYCLE_EDIT = FUNCV_PREFIX + ".用户.回收站.编辑权";
  public static final String PERMISSION_RECYCLE_UNDELETE = FUNCV_PREFIX + ".用户.回收站.恢复删除权";

  @Override
  public String execute() throws Exception {
    String url = ActionConstants.NOTIMPLEMENTED;
    dd.put("viewName", "ViewUserAdvSys");

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "list";
    if ("list".equals(startnode))
      url = this.executeWithPermissions("list", PE.OR(PE.P(PERMISSION_VIEW), PE
          .P(PERMISSION_RECYCLE_VIEW)));
    else if ("filter".equals(startnode))
      url = this.executeWithPermission("filter", null);
    else if ("create".equals(startnode))
      url = this.executeWithPermission("create", PERMISSION_CREATE);
    else if ("view".equals(startnode))
      url = this.executeWithPermissions("view", PE.OR(PE.P(PERMISSION_VIEW), PE
          .P(PERMISSION_RECYCLE_VIEW)));
    else if ("edit".equals(startnode))
      url = this.executeWithPermissions("edit", PE.OR(PE.P(PERMISSION_EDIT), PE
          .P(PERMISSION_RECYCLE_EDIT)));
    else if ("delete".equals(startnode))
      url = this.executeWithPermission("delete", PERMISSION_DELETE);
    else if ("undelete".equals(startnode))
      url = this.executeWithPermission("undelete", PERMISSION_RECYCLE_UNDELETE);
    else if ("editUserRoles".equals(startnode))
      url = this.executeWithPermissions("editUserRoles", PE.OR(PE.P(PERMISSION_EDIT), PE
          .P(CUser.PERMISSION_FA_ROLES + CPermission.SUFFIX_PERMISSION_EDIT)));
    else if ("saveUserRoles".equals(startnode))
      url = this.executeWithPermissions("saveUserRoles", PE.OR(PE.P(PERMISSION_EDIT), PE
          .P(CUser.PERMISSION_FA_ROLES + CPermission.SUFFIX_PERMISSION_EDIT)));
    else if ("editUserPermissions".equals(startnode))
      url = this.executeWithPermissions("editUserPermissions", PE.OR(PE.P(PERMISSION_EDIT), PE
          .P(CUser.PERMISSION_FA_PERMISSIONS + CPermission.SUFFIX_PERMISSION_EDIT)));
    else if ("saveUserPermissions".equals(startnode))
      url = this.executeWithPermissions("saveUserPermissions", PE.OR(PE.P(PERMISSION_EDIT), PE
          .P(CUser.PERMISSION_FA_PERMISSIONS + CPermission.SUFFIX_PERMISSION_EDIT)));
    else if ("browse".equals(startnode))
      url = this.executeWithPermission("browse", null);
    else if ("dispatch".equals(startnode)) {
      String action = (String) dd.getFormValue("action");
      if ("save".equals(action)) {
        String isRecycle = (String) dd.getFormValue("isRecycle");
        if ("true".equalsIgnoreCase(isRecycle)) {
          String uuid = (String) dd.getFormValue("user_uuid");
          if ("".equals(uuid))
            uuid = null;
          if (uuid == null)
            url = this.executeWithPermission("save", PERMISSION_CREATE);
          else
            url = this.executeWithPermission("save", PERMISSION_EDIT);
        } else
          url = this.executeWithPermission("save", PERMISSION_RECYCLE_EDIT);
      }
    }

    return url;
  }

  public String getPermission(String functionName, String domainUuid, Boolean isRecycle)
      throws BusinessException {
    if (!isRecycle.booleanValue()) {
      if (ViewUser.FUNCTION_CREATE.equals(functionName))
        return PERMISSION_CREATE;
      if (ViewUser.FUNCTION_VIEW.equals(functionName))
        return PERMISSION_VIEW;
      if (ViewUser.FUNCTION_EDIT.equals(functionName))
        return PERMISSION_EDIT;
      if (ViewUser.FUNCTION_DELETE.equals(functionName))
        return PERMISSION_DELETE;
    } else {
      if (ViewUser.FUNCTION_VIEW.equals(functionName))
        return PERMISSION_RECYCLE_VIEW;
      if (ViewUser.FUNCTION_EDIT.equals(functionName))
        return PERMISSION_RECYCLE_EDIT;
      if (ViewUser.FUNCTION_UNDELETE.equals(functionName))
        return PERMISSION_RECYCLE_UNDELETE;
    }
    return null;
  }

  protected String getViewRoleName() {
    return "ViewRoleAdvSys";
  }

  protected void addForceFilter(BSQLSubquery bsq) {
    // Do Nothing
  }
}
